<!doctype html>
<html>

	<head>
		<meta charset="UTF-8">
		<title></title>
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<link href="../css/mui.min.css" rel="stylesheet" />
		<link href="../css/mui.picker.css" rel="stylesheet" />
		<link href="../css/mui.poppicker.css" rel="stylesheet" />
		<link rel="stylesheet" href="../css/address-add.css" />
	</head>

	<body>

		<header class="mui-bar mui-bar-nav">
			<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
			<h1 class="mui-title">新增/编辑地址</h1>
		</header>
		<div class="mui-content">
			<div class="mui-content">
				<ul class="mui-table-view">
					<li class="mui-table-view-cell">
						<a class="mui-navigate-right" id="picker" onclick="picker_city()"></a>
					</li>
					<li class="mui-table-view-cell" style="padding: 0;">
						<input type="text" id="detailedAddress" class="detail-address" placeholder="详细地址" />
					</li>
					<li class="mui-table-view-cell" style="padding: 0;">
						<input type="text" id="postcodes" class="detail-address" placeholder="邮政编码" />
					</li>
					<li class="mui-table-view-cell" style="padding: 0;">
						<input type="text" id="receivePerson" class="detail-address" placeholder="请输入收货人姓名" />
					</li>
					<li class="mui-table-view-cell" style="padding: 0;">
						<input type="text" id="receivePhone" class="detail-address" placeholder="请输入收货人联系电话" />
					</li>
					<li class="mui-table-view-cell" style="padding: 0;" id="isDefaultAddress">
						<input type="checkbox" id="defaultAddress" style="margin-left: 10px;"/><span>设置为默认地址</span>
					</li>
					<li class="mui-table-view-cell" id="delAddress">
						<a onclick="delete_address()">删除地址收货地址</a>
					</li>
				</ul>
				<div id='cityResult3' class="ui-alert"></div>
			</div>
		</div>
		<footer class="mui-bar mui-bar-tab" style="height: 35px;">
			<button class="saveBtn" id="saveBtn">保存收货地址</button>
		</footer>
		<script src="../js/mui.min.js"></script>

		<script src="../js/mui.picker.js"></script>
		<script src="../js/mui.poppicker.js"></script>
		<script src="../js/city.data-3.js" type="text/javascript" charset="utf-8"></script>
		<script type="text/javascript" src="../js/const.js"></script>
		<script type="text/javascript" src="../js/request.js"></script>
		<script type="text/javascript" src="../js/address_service.js"></script>
		<script type="text/javascript" src="../js/jquery-1.8.3.min.js"></script>
		<script type="text/javascript">
			//			var cityInfo = new Object();
			var address = new Object();
			(function($, doc) {
				$.init();
			})(mui, document);

			mui.plusReady(function() {
				var self = plus.webview.currentWebview();
				console.log(self.type);
				console.log(self.type === 'update');
				if(self.type === 'update') { //修改
					get_address_info(self.addressId);
					$("#saveBtn").attr('onclick', 'update_address()');

					$("#delAddress").show(); //显示删除按钮
					return;
				}
				if(self.type === 'add') { //添加
					$("#delAddress").hide(); //不显示删除按钮
					$("#saveBtn").attr('onclick', 'add_address()');
					picker_city(); //弹出城市选择器
					return;
				}

			});

			function picker_city() {
				var _getParam = function(obj, param) {
					return obj[param] || '';
				};
				var cityPicker3 = new mui.PopPicker({
					layer: 3
				});

				cityPicker3.setData(cityData3);
				var pickerText = document.getElementById('picker');
				cityPicker3.show(function(items) {
					pickerText.innerText = _getParam(items[0], 'text') + _getParam(items[1], 'text') + _getParam(items[2], 'text');
					address.province = _getParam(items[0], 'text');
					address.city = _getParam(items[1], 'text');
					address.area = _getParam(items[2], 'text');
				});
			}

			function add_address() {

				if(address.city === null) {
					plus.nativeUI.toast("你还没选择城市");
					return;
				}
				if($("#defaultAddress").is(':checked')) {
					address.defaultAddress = 1;
				} else {
					address.defaultAddress = 0;
				}
				address.detailedAddress = document.getElementById("detailedAddress").value;
				address.postcodes = document.getElementById("postcodes").value;
				address.receivePerson = document.getElementById("receivePerson").value;
				address.receivePhone = document.getElementById("receivePhone").value;

				plus.nativeUI.showWaiting('正在提交');

				var res = addressService.create_address(address);
				plus.nativeUI.closeWaiting();
				if(res.resultCode === REQUEST_SUCCESS_CDOE) {
					var list = plus.webview.currentWebview().opener();
					//触发列表界面的自定义事件（refresh）,从而进行数据刷新  
					mui.fire(list, 'refresh');
					mui.currentWebview.close();

				}
				plus.nativeUI.toast(res.msg);
			}

			function get_address_info(addressId) {
				plus.nativeUI.showWaiting('正在加载');
				var res = addressService.find_address(addressId);
				plus.nativeUI.closeWaiting();
				document.getElementById("detailedAddress").value = res.data.detailedAddress;
				document.getElementById("postcodes").value = res.data.postcodes;
				document.getElementById("receivePerson").value = res.data.receivePerson;
				document.getElementById("receivePhone").value = res.data.receivePhone;
				address.province = res.data.province;
				address.city = res.data.city;
				address.area = res.data.area;
				address.id = res.data.id;
				address.userId = res.data.userId;
				//				address.createTime = res.data.createTime;
				if(res.data.defaultAddress === 1) {
					$("#defaultAddress").attr("checked", true);
				}
				var pickerText = document.getElementById('picker');
				pickerText.innerText = res.data.province + res.data.city + res.data.area;
			}

			function update_address() {
				//				var address = new Object();
				if(address.city === null) {
					plus.nativeUI.toast("你还没选择城市");
					return;
				}
				if($("#defaultAddress").is(':checked')) {
					address.defaultAddress = 1;
				} else {
					address.defaultAddress = 0;
				}

				address.detailedAddress = document.getElementById("detailedAddress").value;
				address.postcodes = document.getElementById("postcodes").value;
				address.receivePerson = document.getElementById("receivePerson").value;
				address.receivePhone = document.getElementById("receivePhone").value;
				plus.nativeUI.showWaiting('正在提交');

				var res = addressService.update_address(address);
				plus.nativeUI.showWaiting('正在提交');

				var res = addressService.create_address(address);
				plus.nativeUI.closeWaiting();
				if(res.resultCode === REQUEST_SUCCESS_CDOE) {
					var list = plus.webview.currentWebview().opener();
					//触发列表界面的自定义事件（refresh）,从而进行数据刷新  
					mui.fire(list, 'refresh');
					mui.currentWebview.close();

				}
				plus.nativeUI.toast(res.msg);
			}

			function get_address_info(addressId) {
				plus.nativeUI.showWaiting('正在加载');
				var res = addressService.find_address(addressId);

				plus.nativeUI.closeWaiting();
				document.getElementById("detailedAddress").value = res.data.detailedAddress;
				document.getElementById("postcodes").value = res.data.postcodes;
				document.getElementById("receivePerson").value = res.data.receivePerson;
				document.getElementById("receivePhone").value = res.data.receivePhone;
				address.province = res.data.province;
				address.city = res.data.city;
				address.area = res.data.area;
				address.id = res.data.id;
				address.userId = res.data.userId;
				//				address.createTime = res.data.createTime;
				if(res.data.defaultAddress === 1) {
					$("#defaultAddress").attr("checked", true);
				}
				var pickerText = document.getElementById('picker');
				pickerText.innerText = res.data.province + res.data.city + res.data.area;
			}

			function update_address() {
				//				var address = new Object();
				if(address.city === null) {
					plus.nativeUI.toast("你还没选择城市");
					return;
				}
				if($("#defaultAddress").is(':checked')) {
					address.defaultAddress = 1;
				} else {
					address.defaultAddress = 0;
				}

				address.detailedAddress = document.getElementById("detailedAddress").value;
				address.postcodes = document.getElementById("postcodes").value;
				address.receivePerson = document.getElementById("receivePerson").value;
				address.receivePhone = document.getElementById("receivePhone").value;
				plus.nativeUI.showWaiting('正在提交');

				var res = addressService.update_address(address);
				plus.nativeUI.closeWaiting();
				if(res.resultCode === REQUEST_SUCCESS_CDOE) {
					var list = plus.webview.currentWebview().opener();
					//触发列表界面的自定义事件（refresh）,从而进行数据刷新  
					mui.fire(list, 'refresh');
					mui.currentWebview.close();

				}
				plus.nativeUI.toast(res.msg);
			}

			function delete_address() {

				var btnArray = ['取消', '确定'];
				mui.confirm('是否要删除该收件地址？', '提示', btnArray, function(e) {
					if(e.index == 1) {
						var res = addressService.delete_address(address.id);
						plus.nativeUI.toast(res.msg);
						if(res.resultCode === REQUEST_SUCCESS_CDOE) {
							var list = plus.webview.currentWebview().opener();
							//触发列表界面的自定义事件（refresh）,从而进行数据刷新  
							mui.fire(list, 'refresh');
							mui.currentWebview.close();
						}
					}
				});
			}
		</script>
	</body>

</html>